NetWire
NetWire is the networking solution provided within Orion. It aims to provide a simple and easy interface for creating remote events, functions, and properties. NetWire is built ontop of Sleitnik's Comm library, but removes the interaction with instances so that you, the end user, never has to worry about instance creation or destruction. NetWire works by connecting two endpoints, identified by a shared namespace
and building remote objects under the hood associated with this namespace.
Creation
In order to utilize NetWire we first must create our endpoints on both server and client
-- test.server.lua
local NetWire = Import("NetWire")
local MyServerWire = NetWire.Server("MyWire")
-- test.client.lua
local NetWire = Import("NetWire")
local MyClientWire = NetWire.Client("MyWire")
Calling NetWire.Server("MyWire")
and NetWire.Client("MyWire")
initializes a new wire with the namespace MyWire
on the server and client and connects them. Fetching a wire will always return the same one so you can easily utilize a single namespace across all your scripts without having to store them somewhere, NetWire acts as this repository for you.
local MyServerWire1 = NetWire.Server("MyWire")
local MyServerWire2 = NetWire.Server("MyWire")
print(MyServerWire1 == MyServerWire2) -- true
Remote Events
To create a new RemoteEvent you simply set an index of the ServerWire to NetWire.createEvent()
.
MyServerWire.MyRemoteEvent = NetWire.createEvent()
NetWire.createEvent()
does not actually return an event object. It returns a symbol that is read by the __index
metamethod internally and calls the ServerWire's method :RegisterEvent("EventName")
where "EventName" is the index you set. The RegisterEvent method can be used as a substitute for setting an index to createEvent.
The following code has the client fire a number to the server, the server then doubles this number and sends it back to the client where it is printed.
Server:
MyServerWire.MyRemoteEvent:Connect(function(plr: Player, num: number)
MyServerWire.MyRemoteEvent:Fire(plr, num * 2)
end)
Client:
MyClientWire.MyRemoteEvent:Connect(print) -- 10
MyClientWire.MyRemoteEvent:Fire(5)
Remote Properties
Remote properties are created the same way as Remote events; the only difference being that you can give it a defaulting value to initialize with.
MyServerWire.MyRemoteProperty = NetWire.createProperty(0)
Remote Functions
To create a remote functions you just have to define a function on one of the ServerWire's indices. When you call a remote function, you must call it as a method. Also note that all remote functions retrurn in the form of a Promise, regardless of the server return value.
Server:
function MyServerWire:MyRemoteFunction(plr: Player, num: number)
return num * 2
end
Client:
MyClientWire:MyRemoteFunction(5):andThen(print) -- 10
fromService
You can generate a NetWire for a Roam Service easily by using the .fromService
function.
Server:
local MyService = {}
MyService.Client = {
TestEvent = NetWire.createEvent()
}
-- A generic exposed remote function
function MyService.Client:TestMethod(plr: Player)
return "Hello World!"
end
function MyService:RoamInit()
NetWire.Server.fromService(MyService) -- initializes the Wire
end
Roam.registerService(MyService, "MyService")
return MyService
Client:
local MyService = NetWire.Client("MyService")
MyService:TestMethod():andThen(print) -- Hello World!
When you run fromService
it will read the name of the service registered to Roam and its Client
table and overwrite it.